<template>
  <div class="main">
    <div class="login">
      <div class="title">
        <span>前后端分离Ele+Vue 阿金</span>
      </div>
      <div class="list">
        <el-input
          prefix-icon="el-icon-user"
          placeholder="请输入账号"
          v-model="dataFrom.username"
        ></el-input>
      </div>
      <div class="list">
        <el-input
          prefix-icon="el-icon-unlock"
          placeholder="请输入密码"
          show-password
          v-model="dataFrom.password"
        ></el-input>
      </div>
      <div class="list">
        <el-input
          prefix-icon="el-icon-document-checked"
          placeholder="请输入验证码"
          v-model="dataFrom.encrypt"
        ></el-input>
        <div class="getCode">
          <span @click="getCode">{{ dataFrom.code }}</span>
        </div>
      </div>
      <div class="btn">
        <button @click="loginClick">登录</button>
      </div>
    </div>
    <div class="copyright">
      Copyright © 2023~2026 www.jst.yyds. All rights reserved.
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dataFrom: {
        username: "admin",
        password: "123456",
        code: "",
        encrypt: ""
      }
    };
  },
  mounted() {
    this.getCode();
  },
  methods: {
    loginClick() {
      if (
        this.dataFrom.username == "admin" &&
        this.dataFrom.password == "123456" &&
        this.dataFrom.encrypt == this.dataFrom.code
      ) {
        localStorage.setItem("username", this.dataFrom.username);
        localStorage.setItem("password", this.dataFrom.password);
        localStorage.setItem("code", this.dataFrom.code);
        this.$message.success("登录成功");
        this.$router.push("./home");
      } else {
        this.$message.error("登录失败");
        this.getCode();
      }
    },
    getCode() {
      var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
      var arr = str.split("");
      var oldStr = "";
      for (var i = 0; i < 4; i++) {
        var n = Math.floor(Math.random() * arr.length);
        oldStr += arr[n];
      }
      this.dataFrom.code = oldStr.substring(oldStr.length - 4);
      this.dataFrom.encrypt = oldStr.substring(oldStr.length - 4);
    }
  }
};
</script>
<style scoped>
.main {
  background: url("../../assets/img/login-bg.jpg");
  background-size: 100% 100%;
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
}
.login {
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  border: 1px solid #ffffff;
  background: #fff;
  width: 300px;
  padding: 30px 30px 50px 30px;
  border-radius: 3px;
}
.login .title {
  text-align: center;
  margin: 0 0 10px 0;
}
.login .title span {
  color: #304b6e;
  font-weight: 400;
  font-size: 20px;
}

.login .list {
  display: flex;
  align-items: center;
  padding: 15px 0;
}
.login .list input {
  border-radius: 3px;
  border: none;
  outline: none;
  color: #999;
  border: 1px solid #bdbdbd;
  font-size: 14px;
  line-height: 50px;
  padding: 0 10px;
  display: block;
  box-sizing: border-box;
  flex: 7;
}

.login .list .getCode span {
  font-size: 20px;
  background: #f5f7fa;
  color: #777;
  border-radius: 4px;
  line-height: 40px;
  border: 1px solid #ccc;
  display: inline-block;
  margin-left: 10px;
  width: 80px;
  text-align: center;
  user-select: none;
  cursor: pointer;
}

.btn {
  display: flex;
  justify-content: flex-end;
  padding-top: 5px;
}
.btn button {
  font-size: 13px;
  color: #fff;
  background: #1890ff;
  outline: none;
  border: none;
  line-height: 40px;
  padding: 0 20px;
  display: inline-block;
  flex: 1;
  cursor: pointer;
  border-radius: 3px;
}
.copyright {
  color: #eee;
  text-align: center;
  position: fixed;
  z-index: 1;
  bottom: 20px;
  left: 0;
  right: 0;
  font-size: 14px;
}
</style>
